var page =1;
$(document).ready(function() {
	$("#"+$("#action").html().trim()).addClass("focus");
	if($("#action").html().trim()=="history"){
		loadMore();
	}
	orderCount($("#Times").val());
	$("#Times").change(function(){
		orderCount($(this).val());
	});
	
	
});

function loadMore() {
	var  userName =$("#userName").html().trim();
	var moreHtml="<div align=\"center\" class=\"orderInfo addMore\"><a href='javascript:void(0); return false;' onclick=\"loadMore();\">更多</a></div>";
	$.ajax({
		type : "POST",
		url : "servlet/OrderServlet",
		data : "action=getOrder&userName="+userName+"&start=" + (page-1)*5 + "&count=5",
		dataType: "json",
		success : function(result) {
			
			$.each(result, function(key, value){
				
				//订单项列表
				var cartList = value.cartList;
				//订单项
				var cartHtml= "";
				//总价格
				var totalPrice=0;
				$.each(cartList, function(key1, value1){
					var state = "未提交";
					switch(value1.itemState){
						case 1:	state="已提交[货到付款]";break;
						case 2: state="已提交[已付款]";break;
						case 3: state="商家正在备单";break;
						case 4: state="商家已送货";break;
					}
					totalPrice += value1.itemCount*value1.goodsPrice;
					cartHtml+="<tr class=\"cart_item\"><td class=\"name\"><div style=\"position: relative;\"><a href=\"goodsDetail.jsp?goodsId="+value1.goodsId+"\" target=\"_blank\">"+value1.goodsName+"</a> "
					+"<a class=\"sellerName\"	href=\"menu.jsp?sellerId="+value1.sellerId+"\" target=\"_blank\">【"+value1.sellerName+"】</a><div>状态：<span>"+state+"</span></div><div class=\"itemTime\">"+value1.itemTime+"</div></div></td>"
					+"<td align=\"center\" class=\"number_box\">"+value1.itemCount+"(单价：<span>¥"+value1.goodsPrice+"</span>)</td>"
					+"<td class=\"count_price_box\"><span>¥"+value1.itemCount*value1.goodsPrice+"</span></td></tr>";
				});
				var orderDetail="<div id='"+value.id+"' class='cart' style='display:none;'><div class=\"cart_products\"><table width=\"100%\"><tbody><tr><th width=\"50%\">产品</th><th width=\"25%\">数量</th><th width=\"25%\">价格</th></tr>"
						+cartHtml+"<tr class=\"no_border\"><td colspan=\"6\" class=\"count\">应付总额 ：<span class=\"totalPrice\">¥ <span>"+totalPrice+"</span></span></td></tr>"
						+"</tbody></table></div></div>";
				
				var orderHtml ="<div class=\"orderInfo\" ><div>订 单 号 ：<span class=\"orderNo\">"+value.no+"</span></div>"
				+"<div style=\"float: left;width: 48%;\">收 餐 人 ：<span>"+value.toUser+"</span></div>"
				+"<div>送餐地址：<span>"+value.toAddr+"</span></div>"
				+"<div style=\"float: left;width: 48%;\">订餐日期：<span>"+value.time+"</span></div>"
				+"<div><span>&nbsp;</span><a class='detail' href='javascript:void(0);return false;' onclick='showDetail("+value.id+")' style='float:right;'>详情</a></div>"+orderDetail+"</div>";
			   	$("#orderList").append(orderHtml);
			});
			$(".addMore").remove();
			if(result.length<5){
				//数据获得完毕啦。
				$("#orderList").append("<div align=\"center\" class=\"orderInfo\"><a>数据已加载完成</a></div>");
			}else{
				$("#orderList").append(moreHtml);
			}
		}
	});
	page++;
}

/*显示商品详情*/
function showDetail(id){
	$("#"+id).toggle("slow");
}

/*统计*/
function orderCount(value){
	var userName  = $("#userName").html().trim();
	$("#TimesVal").html($(":selected").html().trim());
	$.ajax({
		type : "POST",
		url : "servlet/OrderServlet",
		data : "action=orderCount&userName="+userName+"&times="+value,
		success : function(result) {
			var arr=new Array();
			arr=result.split(',');//注split可以用字符或字符串分割
			$("#CountAllVal").html(arr[0]);
			$("#PayAllVal").html(arr[1]);
			$("#PayAvgVal").html(arr[2]);
			$("#FavSelVal").html(arr[3]);
			$("#FSCountVal").html(arr[4]);
			$("#FavGoodsVal").html(arr[5]);
			$("#FGCountVal").html(arr[6]);
		}
	});
}

/*收藏夹中的物品加入购物车//购买商品按钮*/
function readyBuy(goodsId){
	var userName = $("#userName").html().trim();
	buy(userName,goodsId);
};

/*将收藏夹中的物品移除//购买商品按钮*/
function deleteCollect(goodsId){
	var userName = $("#userName").html().trim();
	if(userName=="null"){
		window.location.href="logReg.jsp?action=login";
	}else{
		$.ajax({
			type : "POST",
			url : "servlet/CollectServlet",
			data : "action=delete&goodsId="+goodsId+"&userName=" +userName,
			success : function(result) {
				var result= result.trim();
				if(result=="success"){
					window.location.reload();
				}else{
					alert("商品移出收藏夹失败，请重试！");
				}
			}
		});
	}
};
